package com.wang.back.controller;

import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.web.controller.BaseController;
import com.wang.sys.pojo.AtSysConfig;
import com.wang.sys.service.AtSysConfigService;
import com.wang.users.pojo.AtRbacAdmins;
import com.wang.users.pojo.AtRbacAdminsQuery;
import com.wang.users.service.AtRbacAdminsService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Objects;

/**
 * 不登录登录就能访问的页面
 *
 * @author ZjxMi
 */
@Controller
@RequestMapping("/noLogin")
@Log4j2
public class NoLoginController extends BaseController {
    @Autowired
    private AtRbacAdminsService adminsService;
    @Autowired
    private AtSysConfigService sysConfigService;

    /**
     * 登录提交页面
     */
    @RequestMapping("/adminsLoginSubmit")
    public String adminsLoginSubmit(HttpSession session, String email, String password, String code,
                                    Model model, HttpServletRequest request) {
        log.info("==adminsLoginSubmit==");
        String info = "";
        /* 接收参数 */
        /* 获取到图片中的code */
        String sessionCode = session.getAttribute("rand") + "";
        log.info("==email==" + email);
        log.info("==password==" + password);
        log.info(code + "==password==" + password);
        if (email == null || "".equalsIgnoreCase(email)) {
            info = "请输入邮箱";
        } else {
            /* 万能验证码 */
            AtSysConfig configCode = this.sysConfigService.getById(ConstatFinalUtil.FINAL_CONFIG_CODE);
            if (sessionCode.equalsIgnoreCase(code) || configCode.getValStr().equalsIgnoreCase(code)) {
                /* 这叫query对象 */
                AtRbacAdminsQuery adminQuery = new AtRbacAdminsQuery();
                AtRbacAdmins adminsParam = adminQuery.getEntity();
                adminsParam.setEmail(email);
                AtRbacAdmins admins = this.adminsService.findOne(adminQuery);
                /* 关联对象 */
                if (Objects.nonNull(admins)) {
                    /* 根据邮箱查询到了===验证密码 */
                    if (this.encryptUtil.match(admins.getPassword(), password)) {
                        /* 密码正确==看状态 */
                        if (BasePojoEnum.STATUS_ENABLE.getCode().equalsIgnoreCase(admins.getStatus())) {
                            /* 登录成功 */
                            /* 发票 */
                            session.setAttribute("adminsSess", admins);
                            session.setAttribute("lastLoginTime", admins.getLastLoginTime());
                            /* 更新上次登录时间 */
                            admins.setLastLoginTime(new Date());
                            boolean dbFlag = this.adminsService.updateById(admins);
                            log.info("==管理员操作数据库结果:=={}", dbFlag);
                            return "redirect:/back/adminsBack/main";
                        } else {
                            info = "账号被禁用,请联系管理员";
                        }
                    } else {
                        /* 密码不正确 */
                        info = "密码不正确";
                    }
                    model.addAttribute("email", email);
                } else {
                    /* 邮箱填写不正确 */
                    info = "邮箱填写不正确";
                }
            } else {
                /* 验证码不正确 */
                info = "验证码不正确";
            }
        }
        model.addAttribute("info", info);
        return this.adminsLogin(session, request);
    }

    /**
     * 打开登录页面
     */
    @RequestMapping("/adminsLogin")
    public String adminsLogin(HttpSession session, HttpServletRequest request) {
        log.info("==adminsLogin==");
        /* 获取对象,此对象是登录的时候放进去;
         * 如果有,说明登录了.(买票)
         * 如果没有,说明没有登录,(没有买票,去买票)
         *  */
        AtRbacAdmins admins = (AtRbacAdmins) session.getAttribute("adminsSess");
        if (admins != null) {
            /* 说明用户肯定是登录了 */
            return "redirect:/back/adminsBack/main";
        }
        return "back/adminsLogin";
    }
}